package com.cnksi.core.tools.utils;


public class JStringUtils
{
	/**
	* The empty String {@code ""}.
	* @since 2.0
	*/
	public static final String EMPTY = "";

	/**
	 * Represents a failed index search.
	 * @since 2.1
	 */
	public static final int INDEX_NOT_FOUND = -1;

	/**
	* <p>Checks if a CharSequence is empty ("") or null.</p>
	*
	* <pre>
	* StringUtils.isEmpty(null)      = true
	* StringUtils.isEmpty("")        = true
	* StringUtils.isEmpty(" ")       = false
	* StringUtils.isEmpty("bob")     = false
	* StringUtils.isEmpty("  bob  ") = false
	* </pre>
	*
	* <p>NOTE: This method changed in Lang version 2.0.
	* It no longer trims the CharSequence.
	* That functionality is available in isBlank().</p>
	*
	* @param cs  the CharSequence to check, may be null
	* @return {@code true} if the CharSequence is empty or null
	* @since 3.0 Changed signature from isEmpty(String) to isEmpty(CharSequence)
	*/
	public static boolean isEmpty(CharSequence cs)
	{
		return cs == null || cs.length() == 0;
	}

	/**
	* <p>Checks if a CharSequence is not empty ("") and not null.</p>
	*
	* <pre>
	* StringUtils.isNotEmpty(null)      = false
	* StringUtils.isNotEmpty("")        = false
	* StringUtils.isNotEmpty(" ")       = true
	* StringUtils.isNotEmpty("bob")     = true
	* StringUtils.isNotEmpty("  bob  ") = true
	* </pre>
	*
	* @param cs  the CharSequence to check, may be null
	* @return {@code true} if the CharSequence is not empty and not null
	* @since 3.0 Changed signature from isNotEmpty(String) to isNotEmpty(CharSequence)
	*/
	public static boolean isNotEmpty(CharSequence cs)
	{
		return !isEmpty(cs);
	}

	/**
	 * <p>Checks if a CharSequence is whitespace, empty ("") or null.</p>
	 *
	 * <pre>
	 * StringUtils.isBlank(null)      = true
	 * StringUtils.isBlank("")        = true
	 * StringUtils.isBlank(" ")       = true
	 * StringUtils.isBlank("bob")     = false
	 * StringUtils.isBlank("  bob  ") = false
	 * </pre>
	 *
	 * @param cs  the CharSequence to check, may be null
	 * @return {@code true} if the CharSequence is null, empty or whitespace
	 * @since 2.0
	 * @since 3.0 Changed signature from isBlank(String) to isBlank(CharSequence)
	 */
	public static boolean isBlank(CharSequence cs)
	{
		int strLen;
		if (cs == null || (strLen = cs.length()) == 0)
		{
			return true;
		}
		for (int i = 0; i < strLen; i++)
		{
			if (Character.isWhitespace(cs.charAt(i)) == false)
			{
				return false;
			}
		}
		return true;
	}

	/**
	 * <p>Checks if a CharSequence is not empty (""), not null and not whitespace only.</p>
	 *
	 * <pre>
	 * StringUtils.isNotBlank(null)      = false
	 * StringUtils.isNotBlank("")        = false
	 * StringUtils.isNotBlank(" ")       = false
	 * StringUtils.isNotBlank("bob")     = true
	 * StringUtils.isNotBlank("  bob  ") = true
	 * </pre>
	 *
	 * @param cs  the CharSequence to check, may be null
	 * @return {@code true} if the CharSequence is
	 *  not empty and not null and not whitespace
	 * @since 2.0
	 * @since 3.0 Changed signature from isNotBlank(String) to isNotBlank(CharSequence)
	 */
	public static boolean isNotBlank(CharSequence cs)
	{
		return !isBlank(cs);
	}

	/**
	* <p>Removes control characters (char &lt;= 32) from both
	* ends of this String, handling {@code null} by returning
	* {@code null}.</p>
	*
	* <p>The String is trimmed using {@link String#trim()}.
	* Trim removes start and end characters &lt;= 32.
	* To strip whitespace use {@link #strip(String)}.</p>
	*
	* <p>To trim your choice of characters, use the
	* {@link #strip(String, String)} methods.</p>
	*
	* <pre>
	* StringUtils.trim(null)          = null
	* StringUtils.trim("")            = ""
	* StringUtils.trim("     ")       = ""
	* StringUtils.trim("abc")         = "abc"
	* StringUtils.trim("    abc    ") = "abc"
	* </pre>
	*
	* @param str  the String to be trimmed, may be null
	* @return the trimmed string, {@code null} if null String input
	*/
	public static String trim(String str)
	{
		return str == null ? null : str.trim();
	}

	/**
	 * <p>Removes control characters (char &lt;= 32) from both
	 * ends of this String returning {@code null} if the String is
	 * empty ("") after the trim or if it is {@code null}.
	 *
	 * <p>The String is trimmed using {@link String#trim()}.
	 * Trim removes start and end characters &lt;= 32.
	 * To strip whitespace use {@link #stripToNull(String)}.</p>
	 *
	 * <pre>
	 * StringUtils.trimToNull(null)          = null
	 * StringUtils.trimToNull("")            = null
	 * StringUtils.trimToNull("     ")       = null
	 * StringUtils.trimToNull("abc")         = "abc"
	 * StringUtils.trimToNull("    abc    ") = "abc"
	 * </pre>
	 *
	 * @param str  the String to be trimmed, may be null
	 * @return the trimmed String,
	 *  {@code null} if only chars &lt;= 32, empty or null String input
	 * @since 2.0
	 */
	public static String trimToNull(String str)
	{
		String ts = trim(str);
		return isEmpty(ts) ? null : ts;
	}

	/**
	* <p>Removes control characters (char &lt;= 32) from both
	* ends of this String returning an empty String ("") if the String
	* is empty ("") after the trim or if it is {@code null}.
	*
	* <p>The String is trimmed using {@link String#trim()}.
	* Trim removes start and end characters &lt;= 32.
	* To strip whitespace use {@link #stripToEmpty(String)}.</p>
	*
	* <pre>
	* StringUtils.trimToEmpty(null)          = ""
	* StringUtils.trimToEmpty("")            = ""
	* StringUtils.trimToEmpty("     ")       = ""
	* StringUtils.trimToEmpty("abc")         = "abc"
	* StringUtils.trimToEmpty("    abc    ") = "abc"
	* </pre>
	*
	* @param str  the String to be trimmed, may be null
	* @return the trimmed String, or an empty String if {@code null} input
	* @since 2.0
	*/
	public static String trimToEmpty(String str)
	{
		return str == null ? EMPTY : str.trim();
	}

	// Equals
	//-----------------------------------------------------------------------
	/**
	 * <p>Compares two CharSequences, returning {@code true} if they are equal.</p>
	 *
	 * <p>{@code null}s are handled without exceptions. Two {@code null}
	 * references are considered to be equal. The comparison is case sensitive.</p>
	 *
	 * <pre>
	 * StringUtils.equals(null, null)   = true
	 * StringUtils.equals(null, "abc")  = false
	 * StringUtils.equals("abc", null)  = false
	 * StringUtils.equals("abc", "abc") = true
	 * StringUtils.equals("abc", "ABC") = false
	 * </pre>
	 *
	 * @see java.lang.String#equals(Object)
	 * @param cs1  the first CharSequence, may be null
	 * @param cs2  the second CharSequence, may be null
	 * @return {@code true} if the CharSequences are equal, case sensitive, or
	 *  both {@code null}
	 * @since 3.0 Changed signature from equals(String, String) to equals(CharSequence, CharSequence)
	 */
	public static boolean equals(CharSequence cs1, CharSequence cs2)
	{
		return cs1 == null ? cs2 == null : cs1.equals(cs2);
	}

}
